//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by Snow on 16/7/26.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#define kReuse @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *nameArray;
//选中下标的数组
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
//选中名字的数组
@property (nonatomic, strong) NSMutableArray <NSString *> *selectNameArray;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectIndexArray = [NSMutableArray array];
    self.selectNameArray = [NSMutableArray array];
    NSArray *array = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:array];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReuse];
    //tableview 编辑
    //1.设置右barbuttonitem 为编辑的editbuttonitem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
}
- (void)editAction{
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
}
#pragma mark - tableView Delegate
//4.返回cell的编辑状态(样式) 默认为delete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //默认delete 设置tableView编辑风格 多选功能 或运算
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editing) {
        //在全选状态下 数组中移除对应选中的下标
        [_selectIndexArray addObject:indexPath];
        //在全选状态下,数组中添加对应选中的名字
        [_selectNameArray addObject:[_nameArray objectAtIndex:indexPath.row]];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editing) {
        
    }
}

#pragma mark - tableView DataSource
//2.让tableView进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        //要删除选中的数组:
        //1.删除数据源
        [_nameArray removeObjectsInArray:_selectNameArray];
        //2.删除cell
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationLeft];
        //清空选中的下标和名字数组中的元素
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
        NSLog(@"完成");
    }
}
//3.控制tableView能否进行编辑 no为不可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;//可编辑
}
//对应按钮的点击方法
//5.滑动删除 只和这个方法有关
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    //如果对tableView进行添加 删除 先修改数据源 在进行对应的添加删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        //修改数据源
        [_nameArray removeObjectAtIndex:indexPath.row];
        //2.对应修改cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    } else if(editingStyle == UITableViewCellEditingStyleInsert) {
        [_nameArray insertObject:@"wanghuang" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        NSLog(@"插入");
    }
    //tableView无论删除还是添加 都已经进行了新的操作
    
}
//1.可以移动 默认是可以移动的额
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//2.移动完成,更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    NSLog(@"%@", _nameArray);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
